<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>论坛后台管理系统</h2>
        <p>请登录您的管理员账户</p>
      </div>
      
      <form class="login-form" @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="username">邮箱</label>
          <input 
            type="text" 
            id="username" 
            v-model="loginForm.username" 
            placeholder="请输入邮箱"
            required
          >
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input 
            type="password" 
            id="password" 
            v-model="loginForm.password" 
            placeholder="请输入密码"
            required
          >
        </div>
        
        <div class="form-options">
          <label class="remember-me">
            <input type="checkbox" v-model="loginForm.remember">
            <span>记住我</span>
          </label>
          <a href="#" class="forgot-password">忘记密码?</a>
        </div>
        
        <button type="submit" class="login-btn" :disabled="isLoading">
          {{ isLoading ? '登录中...' : '登录' }}
        </button>
        
        <!-- 错误信息显示 -->
        <div v-if="errorMessage" class="error-message">
          {{ errorMessage }}
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { userApi } from '@/api'

const router = useRouter()

// 登录表单数据
const loginForm = ref({
  username: '', // 将作为email参数传递
  password: '',
  remember: false
})

// 加载状态
const isLoading = ref(false)

// 错误信息
const errorMessage = ref('')

// 处理登录
const handleLogin = async () => {
  isLoading.value = true
  errorMessage.value = ''
  
  try {
    // 调用登录接口
    const response = await userApi.loginByPwd({
      email: loginForm.value.username,
      password: loginForm.value.password
    })
    
    // 登录成功处理
    if (response.code === 200 || response.success) {
      // 保存登录状态和token到localStorage
      localStorage.setItem('isLoggedIn', 'true')
      localStorage.setItem('username', loginForm.value.username)
      
      // 如果返回了token，保存token
      if (response.data && response.data.token) {
        localStorage.setItem('token', response.data.token)
      } else {
        // 如果没有返回token，使用一个测试token
        const testToken = 'test-token-' + Date.now()
        localStorage.setItem('token', testToken)
      }
      
      // 跳转到仪表盘
      router.push('/dashboard')
    } else {
      // 登录失败，显示错误信息
      errorMessage.value = response.message || '登录失败，请检查用户名和密码'
    }
  } catch (error) {
    console.error('登录失败:', error)
    // 根据错误类型显示不同的错误信息
    if (error.response) {
      const status = error.response.status
      if (status === 401) {
        errorMessage.value = '用户名或密码错误'
      } else if (status === 500) {
        errorMessage.value = '服务器错误，请稍后再试'
      } else {
        errorMessage.value = error.response.data?.message || '登录失败，请检查网络连接'
      }
    } else {
      errorMessage.value = '网络连接失败，请检查网络设置'
    }
  } finally {
    isLoading.value = false
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4e8f0 100%);
  font-family: 'Arial', sans-serif;
}

.login-box {
  width: 400px;
  padding: 40px;
  background-color: #ffffff;
  border-radius: 10px;
  box-shadow: 0 15px 35px rgba(50, 50, 93, 0.1), 0 5px 15px rgba(0, 0, 0, 0.07);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: #2c3e50;
  font-size: 24px;
  margin-bottom: 10px;
  font-weight: 600;
}

.login-header p {
  color: #64748b;
  font-size: 14px;
}

.login-form {
  display: flex;
  flex-direction: column;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #334155;
  font-size: 14px;
  font-weight: 500;
}

.form-group input {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  font-size: 14px;
  transition: border-color 0.3s, box-shadow 0.3s;
  box-sizing: border-box;
}

.form-group input:focus {
  border-color: #3b82f6;
  outline: none;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 25px;
  font-size: 14px;
}

.remember-me {
  display: flex;
  align-items: center;
  color: #64748b;
  cursor: pointer;
}

.remember-me input {
  margin-right: 6px;
}

.forgot-password {
  color: #3b82f6;
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s;
}

.forgot-password:hover {
  color: #2563eb;
  text-decoration: underline;
}

.login-btn {
  width: 100%;
  padding: 12px;
  background-color: #3b82f6;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.3s;
}

.login-btn:hover {
  background-color: #2563eb;
}

.login-btn:disabled {
  background-color: #94a3b8;
  cursor: not-allowed;
}

.error-message {
  margin-top: 16px;
  padding: 12px;
  background-color: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 6px;
  color: #dc2626;
  font-size: 14px;
  text-align: center;
}
</style>